package com.zego.wawaji.ui.center.wawa;

import android.content.Context;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zego.wawaji.R;
import com.zego.wawaji.base.module.ModuleMgr;
import com.zego.wawaji.base.observer.MsgMgr;
import com.zego.wawaji.base.observer.MsgType;
import com.zego.wawaji.base.utils.StringUtils;
import com.zego.wawaji.http.request.HttpResponse;
import com.zego.wawaji.http.request.RequestComplete;
import com.zego.wawaji.third.image.ImageLoader;
import com.zego.wawaji.ui.base.ExBaseAdapter;
import com.zego.wawaji.ui.bean.wawa.MyWawa;
import com.zego.wawaji.ui.bean.wawa.Wawa;
import com.zego.wawaji.ui.dialog.PickerDialogUtil;
import com.zego.wawaji.ui.dialog.SimpleTipDialog;
import com.zego.wawaji.wawaji_client.App;

import java.util.List;

/**
 * 我的娃娃：寄存中
 * Created by Su on 2017/11/26.
 */
public class UntreatedAdapter extends ExBaseAdapter<MyWawa> {

    public UntreatedAdapter(Context context, List<MyWawa> datas) {
        super(context, datas);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        MyViewHolder holder;
        if (convertView == null) {
            convertView = inflate(R.layout.my_wawa_untreated_item);
            holder = new MyViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (MyViewHolder) convertView.getTag();
        }
        final MyWawa info = getItem(position);
        final Wawa wawa = ModuleMgr.getWawaMgr().getWawa(info.getWwTypeId());

        ImageLoader.loadRoundAvatar(getContext(), wawa.getImg(), holder.wawa_icon);
        holder.wawa_name.setText(wawa.getName());
        holder.time_info.setText(StringUtils.getCreateTime(info.getCreateTime()));
        holder.exchangeNum.setText("可兑" + wawa.getExchange() + "娃娃币");
        holder.btn_send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ModuleMgr.getWawaMgr().reqSendWawa(info.getId(), new RequestComplete() {
                    @Override
                    public void onRequestComplete(HttpResponse response) {
                        if (response.isOk()) {
                            MsgMgr.getInstance().sendMsg(MsgType.MT_Wawa_Untreated, null);
                        }
                    }
                });
            }
        });

        // 兑换
        holder.btn_exchange.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PickerDialogUtil.showSimpleTipDialogExt((FragmentActivity) App.getActivity(), new SimpleTipDialog.ConfirmListener() {
                    @Override
                    public void onCancel() {
                    }

                    @Override
                    public void onSubmit() {
                        reqExchange(info);
                    }
                }, "确定把娃娃换成娃娃币吗？", "温馨提示", "取消", "确定", true, R.color.text_gray);
            }
        });
        return convertView;
    }

    private void reqExchange(MyWawa info) {
        ModuleMgr.getWawaMgr().reqExchangeWawa(info.getId(), new RequestComplete() {
            @Override
            public void onRequestComplete(HttpResponse response) {
                if (response.isOk()) {
                    MsgMgr.getInstance().sendMsg(MsgType.MT_Wawa_Untreated, null);
                    MsgMgr.getInstance().sendMsg(MsgType.MT_Update_MyInfo, null);
                    Exchange exchange = (Exchange) response.getBaseData();
                    if (exchange != null) {
                        PickerDialogUtil.showWawaExchangeDialog((FragmentActivity) getContext(), exchange.getExchangeNum());
                    }
                }
            }
        });
    }

    private class MyViewHolder {
        private ImageView wawa_icon;
        private TextView wawa_name, time_info, exchangeNum;
        private RelativeLayout btn_send;
        private LinearLayout btn_exchange;

        public MyViewHolder(View convertView) {
            initView(convertView);
        }

        private void initView(View convertView) {
            wawa_icon = (ImageView) convertView.findViewById(R.id.wawa_icon);
            wawa_name = (TextView) convertView.findViewById(R.id.wawa_name);
            time_info = (TextView) convertView.findViewById(R.id.time_info);
            btn_send = (RelativeLayout) convertView.findViewById(R.id.btn_send);
            btn_exchange = (LinearLayout) convertView.findViewById(R.id.btn_exchange);
            exchangeNum = (TextView) convertView.findViewById(R.id.exchange_num);
        }
    }
}
